from goods_info.models import GoodsCategoryModel
from rest_framework import serializers


class CategorySerializer(serializers.ModelSerializer):
    """
    商品类别的序列化器
    """
    # 树形展示
    children = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = GoodsCategoryModel
        fields = ['id', 'number_code', 'remark', 'name', 'order_number', 'parent', 'children']

    def get_children(self, obj):
        if obj.children:
            return CategorySerializer(obj.children, many=True).data
        return None
